package cn.hfuu.spark;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.sql.*;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import scala.Function1;

import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class CountCustomersBean {
    public static void main(String[] args) throws AnalysisException {
        // 创建SparkSession
        SparkSession spark = SparkSession.builder()
                .appName("SparkApp")
                .master("local")
                .getOrCreate();
        // 假设有一个结构类型


        // 创建JavaSparkContext
        //JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());
        Dataset<Row> csv = spark.read()
                .option("header", "false") // 假设CSV没有标题行

                .csv("file:///usr/local/customers.csv");
        List<Customer> cs = csv.toJavaRDD().map(m -> {
            return new Customer(Long.parseLong(m.getString(0)), m.getString(1), m.getString(2), m.getString(3),
                    m.getString(4),
                    m.getString(5), m.getString(6), m.getString(7), m.getString(8));

        }).collect();
        cs.forEach(x -> {
            System.out.println(x);
        });
        spark.stop();
    }
}
